<?php

class Core_Db_Table_Row_Abstract extends Zend_Db_Table_Row
{
	protected $_message = array();
	
	/**
	 * Retrieve true/false message
	 *
	 * @return true/false
	 */
	public function isValid()
  {
    return !$this->hasMessage();
  }

	/**
	 * Retrieve true/false message
	 *
	 * @return true/false
	 */
  public function hasMessage()
  {
    return empty($this->_message)?false:true;
  }
	
	/**
	 * Retrieve message
	 *
	 * @param  string $index The user-specified index.
	 * @return string
	 */
  public function getMessage($index = null)
  {
    return ($index === null)?$this->_message:@$this->_message[$index];
  }
	
  /**
	 * Set message
	 *
	 * @param  string $index The user-specified index.
	 * @return string
	 */
  public function setMessage($key, $val = null)
  {
    $messages = is_array($key)?$key:array($key=>$val);
    foreach($messages as $k=>$v)
		{
      $this->_message[$k] = $v;
		}
    return $this;
  }

  /**
	 * Clear message
	 *
	 * @return this
	 */
  public function clearMessage()
	{
    $this->_message = array();
    return $this;
	}

	/**
	 * Get row field value width append array data
	 *
	 * @param  index  			$index  The index for the data.
	 * @return string / array
	 */
	public function appendData($key, $val = null)
  {
		$data 	= $this->_data;
		$append = is_array($key)?$key:array($key => $val);
    foreach($append as $k => $v)
		{
      $data[$k] = $v;
		}
    return $data;
  }

	/**
	 * Set row field value
	 *
	 * @param  string $columnName The column key.
	 * @param  mixed  $value      The value for the property.
	 * @return void
	 * @throws Zend_Db_Table_Row_Exception
	 */
	public function __set($columnName, $value)
	{
			$columnName = $this->_transformColumn($columnName);
			if (!array_key_exists($columnName, $this->_data)) {
					require_once 'Zend/Db/Table/Row/Exception.php';
					throw new Zend_Db_Table_Row_Exception("Specified column \"$columnName\" is not in the row");
			}
			$this->_data[$columnName] = $value;
			if ($value == @$this->_cleanData[$columnName]) {
				if (isset($this->_modifiedFields[$columnName])) {
						unset($this->_modifiedFields[$columnName]);   
				}
			} else {
				$this->_modifiedFields[$columnName] = true;
			}
	}

	/**
	 * _getTableFromString
	 *
	 * @param string $tableName
	 * @return Zend_Db_Table_Abstract
	 */
	protected function _getTableFromString($tableAlias)
	{
		$tableAliases = $this->getTable()->getDependentTables();
		return Zend_Db_Table_Abstract::getTableFromString(
			@array_key_exists($tableAlias, $tableAliases)?$tableAliases[$tableAlias]:$tableAlias, 
			$this->_table);
		/*
		$tableAliases = $this->getTable()->getTableAliases();
		return @array_key_exists($tableAlias, $tableAliases)
			?$tableAliases[$tableAlias]
			:Zend_Db_Table_Abstract::getTableFromString($tableAlias, $this->_table);
		*/
	}
}